package com.byq.mm.gateway;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.tc.gateway.device.AbstractDevice;
import com.tc.gateway.device.GeneralDevice;
import com.tc.gateway.event.GroupAddRespEvent;

public class DeviceEditActivity extends Activity{
	AssistanceApplication application;
	
    BroadcastReceiver messageReceiver;

	private AbstractDevice device;
	EditText name;
	EditText type;
	CheckBox remote;
	ImageView deviceConfirm;
	ImageView deviceCancel;
   
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,   WindowManager.LayoutParams.FLAG_FULLSCREEN);  
        setContentView(R.layout.device);
        application = (AssistanceApplication)this.getApplication();

		String id = this.getIntent().getExtras().getString("id");
		device = (AbstractDevice)application.getHomeApplication().getGatewayEngine().getDeviceManager().getDevice(id);
		 name = (EditText) findViewById(R.id.name);
		 type = (EditText) findViewById(R.id.type);
		 remote = (CheckBox) findViewById(R.id.remote);
		  deviceConfirm = (ImageView)findViewById(R.id.DeviceConfirm);
		  deviceCancel = (ImageView)findViewById(R.id.DeviceCancel);
		  deviceConfirm.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				extract();
				 Intent intent = new Intent(AssistanceAction.ACTION_DEVICE_CONFIG_UPDATE);
				 
				 sendBroadcast(intent);
				 finish();
				
			}
		});
		  deviceCancel.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					finish();
					
				}
			});
		 fill();
        messageReceiver = new BroadcastReceiver(){

    		@Override
    		public void onReceive(Context context, Intent intent) {
    			
    			if(intent.getAction().equals(AssistanceAction.ACTION_HOME_UPDATE)){
    			
    			}
    			if(intent.getAction().equals(AssistanceAction.ACTION_HOME_ERROR)){
    				Toast.makeText(DeviceEditActivity.this, " hhhh", Toast.LENGTH_SHORT).show();
    				
    			}
    			
    		}
    		
    	};
       
                
                IntentFilter mf = new IntentFilter();
               
        		mf.addAction(AssistanceAction.ACTION_HOME_UPDATE);
        		mf.addAction(AssistanceAction.ACTION_HOME_ERROR);
        		registerReceiver(messageReceiver, mf);	
        		
        		
        		
    }
    void fill(){
    	if(device.getName() != null)
    		name.setText(device.getName());
    	else
    		name.setText(device.toString());
    	type.setText(device.getId() + "");
    	type.setEnabled(false);
    	remote.setChecked(device.getConfig().isExport());
    }

    void extract(){
    	device.setName(name.getText().toString());
    	
    	device.getConfig().setExport(remote.isChecked());
    }
  

  
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if(messageReceiver == null)
			return;
		this.unregisterReceiver(messageReceiver);
		this.messageReceiver = null;
	}
	
}
